package com.cugmarket.controller.user;

import com.cugmarket.dto.Result;
import com.cugmarket.service.UserFollowsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cugmarket.constant.MessageConstant.FOLLOW_SUCCESS;
import static com.cugmarket.constant.MessageConstant.UN_FOLLOW_SUCCESS;

@Slf4j
@RestController
@RequestMapping("/user/follows")
public class UserFollowsController {
    @Resource
    private UserFollowsService userFollowsService;

    /**
     * 关注或取关
     * @param followUserId 被关注人id
     * @param isFollow  true代表关注 false代表取关
     * @return
     */
    @PutMapping("/follow")
    public Result follow(@RequestParam("followUserId") String followUserId, @RequestParam("isFollow") Boolean isFollow) {
        boolean flag = userFollowsService.follow(followUserId, isFollow);
        if (flag && isFollow) {
            return Result.success(FOLLOW_SUCCESS);
        }
        else {
            return Result.success(UN_FOLLOW_SUCCESS);
        }
    }

    /**
     * 判断是否关注了某人
     * @param followUserId
     * @return
     */
    @GetMapping("/follow_or_not")
    public Result isFollow(@RequestParam("followUserId") String followUserId) {
        boolean flag = userFollowsService.isFollow(followUserId);
        return Result.success(flag);
    }
}
